package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.ab.util.AbImageUtil;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.FANGKE;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.bean.VipType;
import com.gouaixin.bee.beeframework2.model.MemberModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.TimeUtil;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/3/30.
 */
public class FangkeAdapter extends BaseAdapter {

    Context context;
    List<FANGKE> datas = new ArrayList<FANGKE>();
    ImageLoader imageLoader;

    boolean isVip = false;
    String jid = "";

    public FangkeAdapter(final Context context) {
        this.context = context;
        imageLoader = ImageLoader.getInstance();


    }

    public void setData(List<FANGKE> data) {
        datas = data;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

//        final ImageView mView = (ImageView) LayoutInflater.from(context).inflate(R.layout.grideview_image,
//                null);
        final ImageView mView = new ImageView(context);

        AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                160);
        mView.setLayoutParams(param);

//        mView.setBackgroundResource(R.drawable.mylogo);


        if (datas.size() != 0) {
            String url = datas.get(position).getJicon();
            mLog.e("访客图片url:" + url);

            imageLoader.loadImage(url, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {

                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {

                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                    mView.setBackground(AbImageUtil.bitmapToDrawable(bitmap));

                    isVip=PreferencesUtils.getBoolean(context, "isVIP", false);

                    mLog.e("isVIP=" + isVip);




                    if (position > 0) {
                        if (isVip == false) {
                            mView.setImageResource(R.drawable.near_visitor_suo);
                            mView.setBackground(AbImageUtil.bitmapToDrawable(blurBitmap(bitmap)));
                        }
                    }

//                    setBitmap(mView,position,bitmap);


//                    if(position>0){
//                        mView.setImageResource(R.drawable.near_visitor_suo);
//                        mView.setBackground(AbImageUtil.bitmapToDrawable(blurBitmap(bitmap)));
//                    }else{
//                        mView.setBackground(AbImageUtil.bitmapToDrawable(bitmap));
//                    }


                }

                @Override
                public void onLoadingCancelled(String s, View view) {

                }
            });


        }


        return mView;
    }


    /**
     * 显示头像的方法
     */
    private void setBitmap(final ImageView mView, int position, final Bitmap bitmap) {
        //1.首先判断是否是第一个
        if (position == 0) {
            mView.setBackground(AbImageUtil.bitmapToDrawable(bitmap));
        }

        //2.判断是否是VIP用户
        MemberModel.setGetUserLister(new MemberModel.GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                long vipEndTime = user.getVipEndTime();
                long currentTime = TimeUtil.getCurrentTime();
                if (currentTime <= vipEndTime) {
                    isVip = true;
                    mLog.e("我是VIP：不设置模糊");
                    mView.setBackground(AbImageUtil.bitmapToDrawable(bitmap));
                    mView.setImageBitmap(null);

                } else {
                    isVip = false;
                    MemberModel.vipSuccess(context, VipType.Type_0);
                    mLog.e("我不是VIP：设置模糊");
                    mView.setImageResource(R.drawable.near_visitor_suo);
                    mView.setBackground(AbImageUtil.bitmapToDrawable(blurBitmap(bitmap)));
                }

            }

            @Override
            public void onFailure(int i, String s) {

            }
        });
        MemberModel.getUSER(context);


    }


    /**
     * 将图片高斯模糊
     *
     * @param bitmap
     * @return
     */
    public Bitmap blurBitmap(Bitmap bitmap) {

        //Let's create an empty bitmap with the same size of the bitmap we want to blur
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(context);

        //Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

        //Set the radius of the blur
        blurScript.setRadius(25.f);

        //Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        //Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);

        //recycle the original bitmap
//        bitmap.recycle();

        //After finishing everything, we destroy the Renderscript.
        rs.destroy();

        return outBitmap;


    }
}
